using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
 
namespace Adoor.Mapping
{
    public class XmlConfigSerializer
    {

        public static XmlConfigSerializer _instance  = null;

        public static XmlConfigSerializer Default
        {
            get { 
                if(_instance==null)
                _instance = new XmlConfigSerializer(); 
                    return _instance;}
        }

        public void Serialize(Stream stream,Assembly  assembly)
        {
            foreach (Type type in assembly.GetTypes())
            {
                Attributes.EntityAttribute[] array = (Attributes.EntityAttribute[])type.GetCustomAttributes(typeof(Attributes.EntityAttribute), true);
 
                 
                IVisitor visitor = new ConfigVisitor(stream);
                
                visitor.Visit(type);   
   
                foreach (Attributes.EntityAttribute entity in array)
                {
                    entity.AcceptVisitor(visitor,null); 

                }

                //classbody
                foreach (MemberInfo member in type.GetMembers())
                {

                    Attributes.BaseAttribute[] bodyArray = (Attributes.EntityAttribute[])member.GetCustomAttributes(typeof(Attributes.BaseAttribute), true);
                    Array.Sort<Attributes.BaseAttribute>(bodyArray);
                    foreach (Attributes.BaseAttribute attribut in bodyArray)
                    {
                        attribut.AcceptVisitor(visitor,member);

                    }
                }
                
            }
        }
    }
}
